package cn.example.recyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Fruit> mlist=new ArrayList<>();
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView=findViewById(R.id.mRecyclerView);
        initImgData();
        //指定线性布局管理器 达到listview的效果
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter adapter = new FruitAdapter(mlist);
        recyclerView.setAdapter(adapter);
    }

    private void initImgData() {
        for (int i = 0; i < 3; i++) {
            Fruit a = new Fruit("a", R.drawable.a);
            Fruit b = new Fruit("b", R.drawable.b);
            Fruit c = new Fruit("c", R.drawable.c);
            Fruit d = new Fruit("d", R.drawable.d);
            Fruit e = new Fruit("e", R.drawable.e);
            mlist.add(a);
            mlist.add(b);
            mlist.add(c);
            mlist.add(d);
            mlist.add(e);
        }
    }
}
